package com.baize.glinkad.manager;

import android.app.Application;
import android.content.Context;

import com.baize.glinkad.adchannel.DefaultADManager;
import com.baize.glinkad.utils.ADTool;
import com.baize.glinkad.utils.config.GlinkConfig;

public class ADManagerFactory {

    public static ADManagerFactory instance;

    public ADManager glinkAD;

    public Context context;

    private ADManagerFactory(){

    }

    public static ADManagerFactory instance() {
        if (instance == null) {
            instance = new ADManagerFactory();
        }
        return instance;
    }

    public void initApplication(Application application) {

        glinkAD = DefaultADManager.instance;

        if (DefaultADManager.instance == null) {
            DefaultADManager.instance = new DefaultADManager();
        }
        glinkAD = DefaultADManager.instance;
        GlinkConfig glinkConfig = ADTool.loadPluginInfoByClass(application);
        if(glinkConfig != null){
            try {
                Class localClass = Class.forName(glinkConfig.getClazz());
                glinkAD = (ADManager) localClass.getDeclaredConstructor(new Class[]{}).newInstance(new Object[]{});
                glinkAD.setConfig(glinkConfig);
            }catch (Exception e){

            }
        }
        this.glinkAD.initApplication(application);
    }

}
